package com.yuan.java.util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 袁恩光 on 2017/10/10.
 */
public class MyUtil {


    /**
     * 把 '_' 紧邻的第一个字母转化为大写
     */
    public static String transform(String str) {
        int de = (int) '_';
        char[] chars = str.toCharArray();
        List<Integer> con = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            if ((int) chars[i] == de) {
                con.add(i + 1);
            }
        }
        for (int index : con) {
            chars[index] = String.valueOf(chars[index]).toUpperCase().charAt(0);
        }
        return String.valueOf(chars).replace("_", "");
    }

    /**
     * 把大字符串中大写的字母转化为小写并且前一位 加上 '_'
     */
    public static String unTransform(String str) {
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (Character.isUpperCase(chars[i])) {
                char low = String.valueOf(chars[i]).toLowerCase().charAt(0);
                sb.append("_");
                sb.append(low);
            } else {
                sb.append(chars[i]);
            }
        }
        return sb.toString();
    }
}
